/*
 * Copyright (C) 2009 Leandro Aparecido <lehphyro@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.metalframework.util;

import java.lang.reflect.*;
import java.util.*;

import org.apache.commons.lang.*;

/**
 * Utilities to deal with javabeans.
 * 
 * @since 1.0
 */
public abstract class JavaBeanUtils {

	private static final String GET_PREFIX = "get";
	private static final String IS_PREFIX = "is";
	private static final String SET_PREFIX = "set";
	
	/**
	 * Indicates if the method is getter.
	 * 
	 * @param method Method to be verified.
	 * @return True if the method is getter.
	 */
	public static boolean isGetter(Method method) {
		if (method.getParameterTypes().length > 0) {
			return false;
		}

		Class<?> returnType = method.getReturnType();
		if (returnType.equals(Void.class)) {
			return false;
		}

		String name = method.getName();
		if (!name.startsWith(GET_PREFIX) && !name.startsWith(IS_PREFIX)) {
			return false;
		}
		
		String fieldName;
		if (name.startsWith(GET_PREFIX)) {
			fieldName = name.substring(GET_PREFIX.length());
		} else {
			fieldName = name.substring(IS_PREFIX.length());
		}
		fieldName = StringUtils.uncapitalize(fieldName);
		
		boolean hasField;
		try {
			method.getDeclaringClass().getDeclaredField(fieldName);
			hasField = true;
		} catch (Exception e) {
			hasField = false;
		}
		
		return hasField;
	}

	/**
	 * Indicates if the method is setter.
	 * 
	 * @param method Method to be verified.
	 * @return True if the method is setter.
	 */
	public static boolean isSetter(Method method) {
		if (method.getParameterTypes().length != 1) {
			return false;
		}
		
		String name = method.getName();
		if (!name.startsWith(SET_PREFIX)) {
			return false;
		}
		
		Class<?> returnType = method.getReturnType();
		if (!returnType.equals(Void.class) &&
			!returnType.equals(void.class)) {
			return false;
		}
		
		return true;
	}
	
	public static String getPropertyNameOfGetter(Method method) {
		if (!isGetter(method)) {
			throw new IllegalArgumentException("Method [" + method + "] is not getter");
		}
		
		String name = method.getName().substring(GET_PREFIX.length());
		return StringUtils.uncapitalize(name);
	}
	
	public static Method getSetterForGetter(Method method) {
		if (!isGetter(method)) {
			throw new IllegalArgumentException("Method [" + method + "] is not getter");
		}
		
		String getterName = method.getName();
		String setterName = SET_PREFIX;
		if (getterName.startsWith(GET_PREFIX)) {
			setterName += getterName.substring(GET_PREFIX.length());
		} else {
			setterName += getterName.substring(IS_PREFIX.length());
		}
		
		try {
			return method.getDeclaringClass().getDeclaredMethod(setterName, method.getReturnType());
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
	
	public static boolean isObjectMethod(Method method) {
		for (Method m : Object.class.getDeclaredMethods()) {
			if (m.getName().equals(method.getName()) &&
				Arrays.equals(m.getParameterTypes(), method.getParameterTypes())) {
				return true;
			}
		}
		return false;
	}
}
